package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ColorVo;
import com.project.bean.ProductBean;
import com.project.bean.ProductESBean;
import com.project.bean.SizeVo;
import com.project.dao.IProductDao;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import com.project.vo.ImgPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
      @Autowired
      private IProductMapper mapper;
      @Autowired
      private IProductDao dao;
      
      @Override
      public ProductBean findProductInfo(String productNum) {
            ProductBean productBean = new ProductBean();
            if (StringUtils.hasLength(productNum)) {
                  QueryWrapper<ProductBean> qw = new QueryWrapper<>();
                  qw.eq("productNum", productNum);
                  qw.eq("putStatus", 0);
                  ProductESBean productESBean = dao.findDistinctByProductNum(productNum);
                  productBean = mapper.selectOne(qw);
                  productBean.setProductESBean(productESBean);
            }
            return productBean;
      }
      
      @Override
      public List<SizeVo> findSizeByProductName(String productName) {
            return mapper.findSizeByProductName(productName);
      }
      
      @Override
      public ImgPriceVo findImgByNameSizeColor(String productName, String productSize, String productColor) {
            ImgPriceVo vo = new ImgPriceVo();
            if (StringUtils.hasLength(productName) && StringUtils.hasLength(productSize) && StringUtils.hasLength(productColor)) {
                  QueryWrapper<ProductBean> qw = new QueryWrapper<>();
                  qw.eq("productName", productName);
                  qw.eq("size", productSize);
                  qw.eq("color", productColor);
                  qw.eq("putStatus", 0);
                  ProductBean product = mapper.selectOne(qw);
                  ProductESBean productESBean = dao.findDistinctByProductNum(product.getProductNum());
                  vo.setNum(product.getNum());
                  vo.setImg(productESBean.getImg());
                  vo.setPrice(product.getSellPrice());
            }
            return vo;
      }
      
      /**
       * 2未美化 3审核中
       * @param pros
       */
      @Override
      public void addPro(List<ProductBean> pros) {
            pros.forEach(n->{
                  QueryWrapper<ProductBean> qw=new QueryWrapper<>();
                  qw.eq("productNum",n.getProductNum());
                  ProductBean productBean=mapper.selectOne(qw);
                  if(productBean==null){
                        n.setPutStatus(2);
                        mapper.insert(n);
                  }
                  
            });
      }
      
      @Override
      public void updateProductStatus(String proNum,int status,double price) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.eq("productNum",proNum);
            ProductBean productBean=mapper.selectOne(qw);
            productBean.setPutStatus(status);
            productBean.setSellPrice(price);
            mapper.updateById(productBean);
      }
      
      @Override
      public IPage<ProductBean> findByBeautify(Integer pageNO,String productNum) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.ge("putStatus",2);
            qw.le("putStatus",3);
            if(productNum!=null&&!productNum.equals("")){
                  qw.eq("productNum",productNum);
            }
            return mapper.selectPage(new Page<>(pageNO,4),qw);
      }
      
      @Override
      public ProductBean findByNameAndColorAndSize(String name, String color, String size) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.eq("productName",name);
            qw.eq("color",color);
            qw.eq("size",size);
            qw.eq("putStatus",0);
            return mapper.selectOne(qw);
      }
      @Override
      public ProductBean findByNameAndColorAndSize2(String name, String color, String size) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.eq("productName",name);
            qw.eq("color",color);
            qw.eq("size",size);
            return mapper.selectOne(qw);
      }
      
 
      
      @Override
      public List<ColorVo> findColorByNameSize(String productName, String productSize) {
            return mapper.findColorByNameSize(productName, productSize);
      }
      @Override
      public List<ProductBean> findPTByType(String type) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            if(type!=null&&!type.equals("")){
                  qw.eq("productType",type);
            }
            qw.eq("productStatus",0);
            qw.eq("putStatus",0);
            List<ProductBean> list=mapper.selectList(qw);
            List<ProductBean> list2=new ArrayList<>();
            list.forEach(n->{
                  n.setProductESBean(dao.findByProductNum(n.getProductNum()));
                  boolean t=true;
                 for(ProductBean p:list2){
                       if(p.getProductName().equals(n.getProductName())){
                             t=false;
                       }
                 }
                 if(t){
                       list2.add(n);
                 }
            });
            return list2;
      }
      
      @Override
      public List<ProductBean> findCXByType(String type) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            if(type!=null&&!type.equals("")){
                  qw.eq("productType",type);
            }
            qw.eq("productStatus",2);
            qw.eq("putStatus",0);
            List<ProductBean> list=mapper.selectList(qw);
            List<ProductBean> list2=new ArrayList<>();
            list.forEach(n->{
                  n.setProductESBean(dao.findByProductNum(n.getProductNum()));
                  boolean t=true;
                  for(ProductBean p:list2){
                        if(p.getProductName().equals(n.getProductName())){
                              t=false;
                        }
                  }
                  if(t){
                        list2.add(n);
                  }
            });
            return list2;
      }
      
     
      
      @Override
      public List<ProductBean> findQXByType(String type) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            if(type!=null&&!type.equals("")){
                  qw.eq("productType",type);
            }
            qw.eq("productStatus",1);
            qw.eq("putStatus",0);
            List<ProductBean> list=mapper.selectList(qw);
            List<ProductBean> list2=new ArrayList<>();
            list.forEach(n->{
                  n.setProductESBean(dao.findByProductNum(n.getProductNum()));
                  boolean t=true;
                  for(ProductBean p:list2){
                        if(p.getProductName().equals(n.getProductName())){
                              t=false;
                        }
                  }
                  if(t){
                        list2.add(n);
                  }
            });
            return list2;
      }
      
      @Override
      public List<String> findAllType() {
            List<ProductBean> list=mapper.selectList(null);
            List<String> list2=new ArrayList<>();
            list.forEach(n->{
                  n.setProductESBean(dao.findByProductNum(n.getProductNum()));
                  boolean t=true;
                  for(String p:list2){
                        if(p.equals(n.getProductType())){
                              t=false;
                        }
                  }
                  if(t){
                        list2.add(n.getProductType());
                  }
            });
            return list2;
      }
      
      @Override
      public List<ProductESBean> keyWord(String ms) {
            List<ProductESBean> l=dao.findByDescribe(ms);
            List<ProductESBean> l2=new ArrayList<>();
            l.forEach(n->{
                  QueryWrapper<ProductBean> qw=new QueryWrapper<>();
                  qw.eq("productNum",n.getProductNum());
                  ProductBean productBean=mapper.selectOne(qw);
                  if(productBean.getProductStatus()==0){
                        l2.add(n);
                  }
            });
            return l2;
      }
      @Override
      public void updateProductStatus(int productStatus, String productNum) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.eq("productNum",productNum);
            ProductBean productBean=mapper.selectOne(qw);
            productBean.setPutStatus(productStatus);
            mapper.updateById(productBean);
      }
      
      @Override
      public ProductBean findById(String productNum) {
            QueryWrapper<ProductBean> qw=new QueryWrapper<>();
            qw.eq("productNum",productNum);
            ProductBean productBean=mapper.selectOne(qw);
            return productBean;
      }
      
      
}

